package com.maxatime.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.maxatime.pojo.StudentGradeRelation;
import com.maxatime.service.StudentGradeRelationService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;

/**
 * @author Mr.Zhao
 * @title: StudentGradeRelationController
 * @projectName phoenix
 * @description: TODO
 * @date 2021/8/10 19:33
 */
@Controller
public class StudentGradeRelationController {
    Logger log= LogManager.getLogger();
     @Autowired
    private StudentGradeRelationService studentGradeRelationService;



    @ResponseBody
    @RequestMapping("/addgradestudent")
    public Integer addgradestudent(@RequestBody StudentGradeRelation stu) {


        StudentGradeRelation s=   studentGradeRelationService.selectKey(stu.getStudentId());

        Calendar now = Calendar.getInstance();//获取时间
        stu.setCreateTime(now.getTime());//当前时间
        stu.setUpdateTime(now.getTime());
         stu.setDeleteFlag("0");

        List<StudentGradeRelation> ss=   studentGradeRelationService.select(stu.getGradeGradeId());

                    log.info("wwwwwwwwww"+stu.getStudentsNumber());
                    log.info("wwwwwwwwwwwwwwww"+stu.getGradeGradeName());
                    log.info(stu.getGradeGradeId());
           if(s==null & ss.size()<stu.getStudentsNumber()){
               return  studentGradeRelationService.insert(stu);

            }else {
               return 2;
           }




    }


    @RequestMapping("/gradeStuDelete")
    @ResponseBody
    public Integer gradeStuDelete(@RequestBody String data) {
        JSONObject jsonObject = JSONObject.parseObject(data);
        String pk = jsonObject.getString("studentId");
        int studentId = Integer.parseInt(pk);


        return studentGradeRelationService.gradeStuDelete(studentId);
    }


}
